package cn.cds.withyou.admin.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ScrollView;

import com.squareup.otto.Subscribe;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cds.withyou.admin.Application;
import cn.cds.withyou.admin.R;
import cn.cds.withyou.admin.actions.ActionsCreator;
import cn.cds.withyou.admin.stores.ArenaStore;
import cn.cds.withyou.admin.ui.widget.CommonTitleBar;
import cn.cds.withyou.lib.model.ArenaDto;
import cn.ljs.android.buses.ActionBus;
import cn.ljs.android.buses.DataBus;
import cn.ljs.android.ui.BaseActivity;
import cn.ljs.android.util.AppUtils;
import cn.ljs.android.util.TUtil;

/**
 * <p> Description: </p>
 * <p/>
 * <p> Copyright: Copyright (c) 2016 </p>
 *
 * @author: Json.Lee
 * @Version: 1.0
 * @CreateTime: 8/5/2016 01:33
 */
public class ArenaAddedActivity extends BaseActivity {

    @Bind(R.id.title_et)
    EditText titleEt;
    @Bind(R.id.phone_et)
    EditText phoneEt;
    @Bind(R.id.address_et)
    EditText addressEt;
    @Bind(R.id.pic_et)
    EditText picEt;
    @Bind(R.id.comment_et)
    EditText commentEt;
    @Bind(R.id.space_count_et)
    EditText spaceCountEt;
    @Bind(R.id.content_scroll)
    ScrollView contentScroll;

    private ActionsCreator mCreator;
    private ActionBus mActionBus;
    private DataBus mDataBus;
    private ArenaStore mStore;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_arena_added);
        ButterKnife.bind(this);

        init();
    }

    private void init () {
        contentScroll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AppUtils.hideKeyBoard(ArenaAddedActivity.this);
                return false;
            }
        });

        mCreator = ActionsCreator.get();
        mActionBus = Application.getInstance().getActionsBus();
        mDataBus = Application.getInstance().getDataBus();
        mStore = ArenaStore.get();

        mDataBus.register(this);
        mActionBus.register(mStore);
    }

    @Subscribe
    public void updateUI (ArenaStore.ArenaEvent event) {
        switch (event.code) {
            case ArenaStore.ArenaEvent.ARENA_ADDED_SUCC:
                TUtil.shortToast("添加成功。");
                finish();
                break;
            case ArenaStore.ArenaEvent.ARENA_ADDED_FAIL:
                TUtil.shortToast("添加失败，请重试。");
                break;
            default:
                return;
        }
    }

    @Override
    protected void onDestroy() {
        mActionBus.unregister(mStore);
        mDataBus.unregister(this);
        super.onDestroy();
    }

    @OnClick({R.id.add_btn})
    public void onClick (View v) {
        switch (v.getId()) {
            case CommonTitleBar.LEFT_BUTTON_ID:
                finish();
                break;
            case R.id.add_btn:
                AppUtils.hideKeyBoard(this);
                ArenaDto arena = getParams();
                if (checkParams(arena)) {
                    mCreator.requestArenaAdded(arena);
                }
                break;
        }
    }

    private boolean checkParams (ArenaDto dto) {
        if (TextUtils.isEmpty(dto.getName())) {
            TUtil.shortToast("请输入球馆名称。");
            return false;
        }
        if (TextUtils.isEmpty(dto.getPhone())) {
            TUtil.shortToast("请输入联系电话。");
            return false;
        }
        if (TextUtils.isEmpty(dto.getAddress())) {
            TUtil.shortToast("请输入球馆地址。");
            return false;
        }
        if (TextUtils.isEmpty(dto.getPic())) {
            TUtil.shortToast("请输入球馆图片链接。");
            return false;
        }
        if (TextUtils.isEmpty(dto.getComment())) {
            TUtil.shortToast("请输入球馆介绍。");
            return false;
        }
        if (dto.getSpaceCount() <= 0) {
            TUtil.shortToast("请输入场地数。");
            return false;
        }
        return true;
    }

    private ArenaDto getParams () {
        ArenaDto arena = new ArenaDto();
        arena.setName(titleEt.getText().toString().trim());
        arena.setPhone(phoneEt.getText().toString().trim());
        arena.setPic(picEt.getText().toString().trim());
        arena.setAddress(addressEt.getText().toString().trim());
        arena.setComment(commentEt.getText().toString().trim());
        int count = -1;
        try{
            count = Integer.parseInt(spaceCountEt.getText().toString().trim());
        } catch (NumberFormatException e) {}
        arena.setSpaceCount(count);
        return arena;
    }
}
